<?php
/**
 * Created by PhpStorm.
 * User: 田家强
 * Date: 2017/3/28
 * Time: 8:40
 */

namespace Home\Controller;
use Org\Util\Cart;
use Think\Controller;
class ContentController extends BaseContoller
{
    public function index()
    {
        $gid = I('get.gid');
        $headConf = ['title'=>'维依尚品_列表页','js'=>['list']];
        $this->assign('headConf',$headConf);
       // $this->assign( 'conf' , $conf );
        //dd($gid);
        //1.类型属性和商品属性关联
        $field = m('typeattr')->alias('ta')
            ->join("__GOODS_ATTR__ ga on ta.taid = ga.taid")
            ->where("gid={$gid} and ta.value=0")->select();
        //dd($field);die;
        $data =[];
        //2.找到商品、属性表里的name值
        foreach($field as $k =>$v)
        {
            $data[$v['name']][] =$v;
        }
        //dd($data);
        //3.开始分配变量【将数据分配到页面上】
        $this->assign('data',$data);

        $goods = m('goods')->alias('tg')
            ->join("__DETAIL__ ag on tg.gid = ag.gid " )->where("ag.gid ={$gid}")->find();
        //将图片切割成数组
        $goods['small']=explode("|",$goods['small']);
        $goods['medium'] =explode("|",$goods['medium']);
        $goods['big']=explode("|",$goods['big']);
//      dd($goods);
        $this->assign('goods',$goods);
        $this->display();
    }

    /**
     * 用异步方式获取库存
     */
    public function ajaxGetTotal()
    {
        if(IS_AJAX){
            $gid = I('post.gid');

            $combine = rtrim(I('post.combine'),',');
            //dd($combine);die;
            //早货品列表中查找数据
            $data = m('goods_list')->where("gid={$gid} and combine='{$combine}'")->find();

            //dd($data);die;
            //判断如果$data为空的话
            if(!$data){
                $this->ajaxReturn(['valid'=>0,'total' => 0]);exit;
            }
            //如果走到这里说明数据库能找到数据
            if ( $data[ 'inventory' ] == 0 ) {
                //说明库存为0
                $this->ajaxReturn( [ 'valid' => 0 , 'total' => 0 ] );
                exit;
            }
            //接下来判断有木有库存 走到这里说明有库存
            $this->ajaxReturn( [ 'valid' => 1 , 'total' => $data[ 'inventory' ] ,'glid'=>$data['glid'],'number'=>['number']] );
            exit;

        }
    }

    /**
     * 异步添加购物车
     */
    public function ajaxAddCart()
    {
        if(IS_AJAX)
        {
            //1.接收提交的数据
            $data['gid'] = $_POST['gid'];
            //商品ID
            $data['num'] = $_POST['num'];
            //购买数量
            $data['options'] =$_POST['options'];
            //获取库存数量
            $data['count'] = $_POST['count'];



            //2.根据商品ID获取商品名称、价格、列表图
            $field = m('goods')->where("gid={$data['gid']}")
                ->field('gname,price,drawing')->find();
            //dd($field);
            $data = [
                'id' =>$data['gid'],//商品ID
                'name' =>$field['gname'],//商品名称
                'num' => $data['num'],//商品数量
                'price' =>$field['price'] , // 商品价格
                'count'=>$data['count'],//库存数量

                'options' => [
                    'count'=> $data['count'],
                    'options'=> $data['options'],
                    'img'=>$field['drawing']
                ]// 其他参数如价格、颜色、可以为数组或字符串
            ];

            //(new \Org\Util\Cart())->add($data);
              (new Cart())->add($data);
              dd($_SESSION);die;
            //echo 1;die;
        }

    }

}


















